#coding:utf-8
from sqlalchemy.sql.expression import func
from dao.dbhelper import DBHelper
from model.order import Order
import time
import datetime


class OrderDao(DBHelper):
    """docstring for OrderDao"""
    def __init__(self):
        super(OrderDao, self).__init__()

    def get_all_today(self):
        today = datetime.datetime.now()
        year = today.year
        month = today.month
        day = today.day

        list = self.session.query(Order).filter(datetime.datetime(year, month, day) < Order.shop_time).all()
        self.session.close()
        return list

    def add_order(self, what, amount, uname):
        order = Order()
        order.shop_time = datetime.datetime.now()
        order.amount = amount
        
        order.item = what
        order.audit_state = 0
        order.status = 0
        order.name = uname

        self.session.add(order)
        self.session.commit()
        return 1

    def update_add(self, id, audit_state):
        old = self.session.query(Order).filter(Order.id == id).first()
        if old:
            old.audit_state = audit_state
            self.session.commit()
            return 1
        return 0

orderdao = OrderDao()
orderdao.set_model(Order)